package com.muyu.system.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.system.domain.DanmoMarketOrderDetails;
import com.muyu.system.service.IDanmoMarketOrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 书画市场订单详情Controller
 *
 * @author kunzong
 * @date 2024-12-04
 */
@RestController
@RequestMapping("/DanmoMarketOrderDetails")
public class DanmoMarketOrderDetailsController extends BaseController {
    @Autowired
    private IDanmoMarketOrderDetailsService danmoMarketOrderDetailsService;

    /**
     * 查询我的书画市场订单详情列表
     */
//    @RequiresPermissions("system:DanmoMarketOrderDetails:list")
    @GetMapping("/list")
    public Result list(DanmoMarketOrderDetails danmoMarketOrderDetails) {
        PageHelper.startPage(danmoMarketOrderDetails.getPageNum(), danmoMarketOrderDetails.getPageSize());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        danmoMarketOrderDetails.setBuyersId(loginUser.getUserid());
        List<DanmoMarketOrderDetails> list = danmoMarketOrderDetailsService.selectDanmoMarketOrderDetailsList(danmoMarketOrderDetails);
        PageInfo<DanmoMarketOrderDetails> danmoMarketProductsPageInfo = new PageInfo<>(list);
        return Result.success(danmoMarketProductsPageInfo);
    }

    /**
     * 导出书画市场订单详情列表
     */
//    @RequiresPermissions("system:DanmoMarketOrderDetails:export")
    @Log(title = "书画市场订单详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoMarketOrderDetails danmoMarketOrderDetails) {
        List<DanmoMarketOrderDetails> list = danmoMarketOrderDetailsService.selectDanmoMarketOrderDetailsList(danmoMarketOrderDetails);
        ExcelUtil<DanmoMarketOrderDetails> util = new ExcelUtil<DanmoMarketOrderDetails>(DanmoMarketOrderDetails.class);
        util.exportExcel(response, list, "书画市场订单详情数据");
    }

    /**
     * 获取书画市场订单详情详细信息
     */
//    @RequiresPermissions("system:DanmoMarketOrderDetails:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return Result.success(danmoMarketOrderDetailsService.selectDanmoMarketOrderDetailsById(id));
    }

    /**
     * 新增书画市场订单详情
     */
//    @RequiresPermissions("system:DanmoMarketOrderDetails:add")
    @Log(title = "书画市场订单详情", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoMarketOrderDetails danmoMarketOrderDetails) {
        return Result.success(danmoMarketOrderDetailsService.insertDanmoMarketOrderDetails(danmoMarketOrderDetails));
    }

    /**
     * 修改书画市场订单详情
     */
//    @RequiresPermissions("system:DanmoMarketOrderDetails:edit")
    @Log(title = "书画市场订单详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoMarketOrderDetails danmoMarketOrderDetails) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return Result.success(danmoMarketOrderDetailsService.updateDanmoMarketOrderDetails(danmoMarketOrderDetails));
    }

    /**
     * 删除书画市场订单详情
     */
//    @RequiresPermissions("system:DanmoMarketOrderDetails:remove")
    @Log(title = "书画市场订单详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return Result.success(danmoMarketOrderDetailsService.deleteDanmoMarketOrderDetailsByIds(ids));
    }

    /**
     * 付款
     */
//    @RequiresPermissions("system:details:edit")
    @Log(title = "付款", businessType = BusinessType.UPDATE)
    @PutMapping("/payment")
    public Result payment(@RequestBody DanmoMarketOrderDetails danmoMarketOrderDetails) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoMarketOrderDetailsService.updateDanmoShoppingOrderDetails(danmoMarketOrderDetails);
    }


    /**
     * 收货
     */
    @Log(title = "收货", businessType = BusinessType.UPDATE)
    @PostMapping("/receiving")
    public Result receiving(@RequestBody DanmoMarketOrderDetails danmoMarketOrderDetails) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoMarketOrderDetailsService.updateDanmoShoppingReceiving(danmoMarketOrderDetails);
    }

    /**
     * 退货/售后
     */
//    @RequiresPermissions("system:cart:edit")
    @Log(title = "收获", businessType = BusinessType.UPDATE)
    @PostMapping("/afterSales")
    public Result afterSales(@RequestBody DanmoMarketOrderDetails danmoMarketOrderDetails) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoMarketOrderDetailsService.updateDanmoShoppingAfterSales(danmoMarketOrderDetails);
    }


    /**
     * 新增物流单号（发货）
     */
//    @RequiresPermissions("system:cart:edit")
    @Log(title = "新增物流单号", businessType = BusinessType.UPDATE)
    @PostMapping("/logisticsOrderNumber")
    public Result logisticsOrderNumber(@RequestBody DanmoMarketOrderDetails danmoMarketOrderDetails) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return Result.notLoggedIn("请登录");
        }
        return danmoMarketOrderDetailsService.updateDanmoShoppingLogisticsOrderNumber(danmoMarketOrderDetails);
    }
}
